package com.app.service.impl;

import com.app.mapper.InterviewRecordMapper;
import com.app.mapper.LookMeMapper;
import com.app.model.*;
import com.app.service.InterviewRecordService;
import com.app.service.JobPublishService;
import com.app.service.LookMeService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.vo.ViewRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Description:面试记录
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
@Log4j2
public class InterviewRecordServiceImpl implements InterviewRecordService {

    @Autowired
    private UserService userService;
    @Autowired
    InterviewRecordMapper interviewRecordMapper;
    @Autowired
    JobPublishService jobPublishService;

    @Override
    public List<InterviewRecordModel> interviewRecordList(String mobile, Integer userType, UserModel user) throws ParseException {

        InterviewRecordModel model = new InterviewRecordModel();
        if (userType == 1) {
            model.setCid(user.getCId());
            UserModel user2 = userService.getUserByMobile(mobile);
            model.setRid(user2.getRId());

        } else if (userType == 2) {
            model.setRid(user.getRId());
            UserModel user2 = userService.getUserByMobile(mobile);
            model.setCid(user2.getCId());
        }
        List<InterviewRecordModel> modelList = getInterviewRecordList(model);
        if(modelList!=null)
        {
            for (int i = 0; i < modelList.size(); i++) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date1 = df.parse(modelList.get(i).getInterviewTime() + ":00");
                Date date2 = df.parse(CommonHelp.getNowTimestamp() + ":00");

                long long1 = date1.getTime();
                long long2 = date2.getTime();
                if (long1 > long2) {
                    modelList.get(i).setTimeTest(2);
                } else {
                    modelList.get(i).setTimeTest(1);
                }
                JobPublishModel publishModel = jobPublishService.findAllJobById(modelList.get(i).getPid());
                modelList.get(i).setJobTitle(publishModel.getJobTitle());
                modelList.get(i).setMinSalary(publishModel.getMinSalary());
                modelList.get(i).setMaxSalary(publishModel.getMaxSalary());
            }
        }else{
            modelList = new ArrayList<>();
        }
        return modelList;
    }

    @Override
    public List<InterviewRecordModel> getInterviewRecordList(InterviewRecordModel model) {
        QueryWrapper<InterviewRecordModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("rid", model.getRid());
        queryWrapper.select().eq("cid", model.getCid());
        queryWrapper.orderByDesc("crt_time");
        List<InterviewRecordModel> userList = interviewRecordMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public InterviewRecordModel getInterviewRecordById(int id) {
        QueryWrapper<InterviewRecordModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        List<InterviewRecordModel> userList = interviewRecordMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public void update(InterviewRecordModel model) {
        interviewRecordMapper.updateById(model);
    }

    @Override
    public int insert(InterviewRecordModel model) {
        return interviewRecordMapper.insert(model);
    }
}
